package com.intelligentmeterreadingsys.otherequipment.activity;

import java.util.ArrayList;
import java.util.List;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.otherequipment.db.EquipementDao;
import com.intelligentmeterreadingsys.povo.OtherEquipmentPOVO;
import com.intelligentmeterreadingsys.util.Model;
import com.intelligentmeterreadingsys.waterlist.querywaterlist.activity.ListViewActivity;
import com.intelligentmeterreadingsys.waterlist.querywaterlist.activity.ListViewAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;

public class EquipementQueryListActivity extends Activity implements OnScrollListener  {
	List<String> items = new ArrayList<String>();
	private ListView listView;
	private int visibleLastIndex = 0; // 最后的可视项索引
	private int visibleItemCount; // 当前窗口可见项总数
	private ListViewAdapter adapter;
	private View loadMoreView;
	private Button loadMoreButton;
	private Handler handler = new Handler();
	private int count = 0;// 总页数
	public EquipementQueryActivityGroup act = null;// 父类
	public List<OtherEquipmentPOVO> list = null;
	private int page = 1;// 当前页数
	private TextView textview;// 页数信息
	private EditText edit;// 输入页数
	private String equipementid;
	public Button button2;// 上一页面;
	public Button button3;// 下一页
	public Model model;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public ListView getListView() {
		return listView;
	}

	public void setListView(ListView listView) {
		this.listView = listView;
	}

	public List<OtherEquipmentPOVO> getList() {
		return list;
	}

	public void setList(List<OtherEquipmentPOVO> list) {
		this.list = list;
	}

	public String getEquipementid() {
		return equipementid;
	}

	public void setEquipementid(String equipementid) {
		this.equipementid = equipementid;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);
        act=(EquipementQueryActivityGroup)getParent();
        loadMoreView = getLayoutInflater().inflate(R.layout.listview_button, null);  
        loadMoreButton = (Button) loadMoreView.findViewById(R.id.listview_ok);
        button2=(Button)loadMoreView.findViewById(R.id.listview_lastpage);
        button3=(Button)loadMoreView.findViewById(R.id.listview_nextpage);
        button3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				button3.setText("正在加载...");   //设置按钮文字loading  
				page++;					
				if(page>=count){
					page=count;
					button3.setText("下一页"); 
					Toast.makeText(EquipementQueryListActivity.this, "已经是最后一页了！！！", 1000).show();
					return;
				}
		        handler.postDelayed(new Runnable() {  
		            @Override  
		            public void run() {  			                
		             flip();			                
		               //visibleLastIndex - visibleItemCount + 1
		                button3.setText("下一页");    //恢复按钮文字  
		            }  
		        }, 1000);  
			
				
			
				
			}
		});
        button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				// TODO Auto-generated method stub
				button2.setText("正在加载...");   //设置按钮文字loading  
				page--;					
				if(page<=0){
					page=1;
					 button2.setText("上一页");
					 Toast.makeText(EquipementQueryListActivity.this, "已经是第一页了！！！", 1000).show();
					return;
				}
		        handler.postDelayed(new Runnable() {  
		            @Override  
		            public void run() {  			                
		             flip();		                
		               //visibleLastIndex - visibleItemCount + 1
		                button2.setText("上一页");    //恢复按钮文字  
		            }  
		        }, 1000);  
			
				
			}
		});
        textview=(TextView)loadMoreView.findViewById(R.id.listview_info);
        edit=(EditText)loadMoreView.findViewById(R.id.listview_pagenumber);
     
       loadMoreButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				loadMoreButton.setText("正在加载...");   //设置按钮文字loading  
				String s=edit.getEditableText().toString();
				if(TextUtils.isEmpty(s)){
					loadMoreButton.setText("确定");
					return;
				}
				page=Integer.parseInt(s);
				if(page<=0){
					Toast.makeText(EquipementQueryListActivity.this, "页数不能小于1！！！", 1000).show();
					loadMoreButton.setText("确定");
					return;
				}
				if(page>=count){
					page=count;
				}
		        handler.postDelayed(new Runnable() {  
		            @Override  
		            public void run() {  			                
		              flip();		                
		               //visibleLastIndex - visibleItemCount + 1
		                loadMoreButton.setText("确定");    //恢复按钮文字  
		            }  
		        }, 1000);  
			}
		});
        listView = (ListView) this.findViewById(R.id.listView1);
          
        listView.addFooterView(loadMoreView);   //设置列表底部视图  
       // listView.addHeaderView(v)    //设置列表顶部视图
        adapter = new ListViewAdapter(items,this);  
        listView.setAdapter(adapter);                //自动为id是list的ListView设置适配器  
        
        listView.setOnScrollListener(this);     //添加滑动监听 
  
      //  initAdapter();  	        
        listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
			 
			}
		});
         
        queryDater();
    }  
      	  
    /** 
     * 滑动时被调用 
     */  
    @Override  
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {  
        this.visibleItemCount = visibleItemCount;  
        visibleLastIndex = firstVisibleItem + visibleItemCount - 1;  
    }  
  
    /** 
     * 滑动状态改变时被调用 
     */  
    @Override  
    public void onScrollStateChanged(AbsListView view, int scrollState) {  
        int itemsLastIndex = adapter.getCount() - 1;    //数据集最后一项的索引  
        int lastIndex = itemsLastIndex + 1;             //加上底部的loadMoreView项  
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {  
            //如果是自动加载,可以在这里放置异步加载数据的代码  
            Log.i("LOADMORE", "loading...");  
        }  
	}
	  /** 
     * 模拟加载数据 
     */  
    private void loadData() {  
    	System.out.println(page);
    	if(count>0){
			
		}else{
			list.clear();
		}
    	
    	if(page>=count){
    		Toast.makeText(EquipementQueryListActivity.this, "已到最后一页了", 1000).show();
    		page=count;
    			
    	}
        if(count==0){	
        	
    		adapter.removeItem();	
    		adapter.notifyDataSetChanged(); //数据集变化后,通知adapter  
    		items.add("没有数据..........");	
    		 listView.setSelection(0); //设置选中项  
             textview.setText("页数"+page+"/"+count+"   跳转到");
             list=null;
             return;
    	}else{
    		adapter.removeItem();	
    		adapter.notifyDataSetChanged(); //数据集变化后,通知adapter  
             
            listView.setSelection(0); //设置选中项  
            textview.setText("页数"+page+"/"+count+"   跳转到");
	  	     
	    	if(count==0){
	    		Toast.makeText(EquipementQueryListActivity.this, "没有查到数据", 1000).show();
	    		items.add("没有数据...............");
	    		list=null;
	    		return ;
	    	}
	    	if(list==null||list.size()<1){
	    		
	    		return;
	    	}      
	    	for(int i=0;i<list.size();i++){
	    		OtherEquipmentPOVO use=list.get(i);
	    		if(use.getIsadd().equals("新增")){
	    			items.add(use.getTypename()+"  "+use.getAdds()+"(新增)");
	    		}else{
	    			items.add(use.getTypename()+"  "+use.getAdds()+"  巡检("+use.getIscheck()+")");
	    		}
        		
	    	}
    	}
    	
    }

    /**
     * 查询数据
     */
	public void queryDater(){
		 count=0;
		 page=1;
		 act=(EquipementQueryActivityGroup)getParent();	
		 act.radio2.setChecked(true);
			EquipementDao dao=new EquipementDao(this);
			model=new Model(page, 10);
			list=dao.queryEquipment(act.getType(),act.getSerchstatus(),act.getQuerystatus(),act.getNumber()
					,act.getAdds(),model);
			count=dao.queryTotalPage(act.getType(),act.getSerchstatus(),act.getQuerystatus(),act.getNumber()
					,act.getAdds(),model);
			dao.close();
			act.setList(list);
		    loadData();
	}
	
	/**
	 * 翻页
	 */
	public void flip(){
		act=(EquipementQueryActivityGroup)getParent();		       
		EquipementDao dao=new EquipementDao(this);
		model=new Model(page, 10);
		list=dao.queryEquipment(act.getType(),act.getSerchstatus(),act.getQuerystatus(),act.getNumber()
				,act.getAdds(),model);
		
		dao.close();
		act.setList(list);
	     loadData();
	}

}
